//
//  MineAdviceViewController.m
//  GiftItems
//
//  Created by lanou3g on 15/9/22.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MineAdviceViewController.h"

@interface MineAdviceViewController ()<UITextFieldDelegate,MFMailComposeViewControllerDelegate>
@property(nonatomic, strong) UILabel *adviceLabel;
@property(nonatomic, strong) UITextField *adviceText;
@property(nonatomic, strong) UILabel *contactLabel;
@property(nonatomic, strong) UITextField *contactText;
@property(nonatomic, strong) UILabel *label;
@property(nonatomic, strong) MFMailComposeViewController *picker;
@end

@implementation MineAdviceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kRGB(255.0, 230.0, 255.0);
    self.title = @"意见反馈";
    [self addRightButtonItem];
    [self addLabelView];
    [self addTextView];
}

// 添加提交按钮
- (void)addRightButtonItem {
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(sendMessage)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
}
// 添加Label
- (void)addLabelView {
    
    self.adviceLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 150, 45)];
    self.adviceLabel.text = @"反馈内容:";
    [self.view addSubview:self.adviceLabel];
    
    self.contactLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 150, 45)];
    self.contactLabel.text = @"联系方式:";
    [self.view addSubview:self.contactLabel];
    
}
// 添加textfiled
- (void)addTextView {
    self.adviceText = [[UITextField alloc] initWithFrame:CGRectMake(20, 60, kWidth - 40, 100)];
    self.adviceText.placeholder = @"我们需要您的建议哦~~~";
    [self setText:self.adviceText];
    
    self.contactText = [[UITextField alloc] initWithFrame:CGRectMake(20, 245, kWidth - 40, 50)];
    self.contactText.placeholder = @"电话/邮箱/QQ";
    [self setText:self.contactText];
    
}

// 设置文本的属性
- (void)setText:(UITextField *)text {
    text.delegate = self;
    
    text.borderStyle = UITextBorderStyleRoundedRect;
    text.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:text];
}

- (void)sendMessage {
    
    if (self.adviceText.text.length == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"你还没有填写内容吆~~" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [alertView show];
    }else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"感谢您的建议!我们会更加努力哒~~" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [alertView show];
        BOOL canSendMail = [MFMailComposeViewController canSendMail];
        if (canSendMail) {
            self.picker = [[MFMailComposeViewController alloc] init];
            [self.picker setSubject:@"意见反馈"];
            [self.picker setToRecipients:[NSArray arrayWithObject:@"lanou3g@163.com"]];
            [self.picker setMessageBody:self.adviceText.text isHTML:YES];
            [self.picker setMailComposeDelegate:self];
            [self presentViewController:self.picker animated:YES completion:nil];
        }
    }
}

#pragma mark -- MFMailComposeViewControllerDelegate的代理方法 --
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    switch (result) {
        case MFMailComposeResultCancelled:
            // 取消发送
            NSLog(@"取消发送");
            break;
        case MFMailComposeResultFailed:
            // 发送失败
            NSLog(@"发送失败");
            break;
        case MFMailComposeResultSaved:
            // 发送保存
            NSLog(@"发送保存");
            break;
        case MFMailComposeResultSent:
            // 发送
            NSLog(@"发送");
            break;
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField endEditing:YES];// 回收键盘的方法
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
